//
//  AIGirlProfileGetViewController.swift
//  AIGirl
//
//  Created by Rae on 2023/12/29.
//

import UIKit

class AIGirlProfileGetViewController: AIGirlBaseViewController {

    private let backButton = c_make_ex(UIButton.self)
        .image("AIGirl_Profile_account_back").end()
    private let titleLabel = c_make_ex(UILabel.self)
        .color(.white).text("Get").font(19.font_bold).alignment(.center).end()
    private lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(AIGirlProfileGetTableViewCell.self)
        tableView.showsVerticalScrollIndicator = false
        tableView.backgroundColor = .clear
        tableView.separatorStyle = .none
        tableView.rowHeight = 210
        return tableView
    }()
    private let emptyView = AIGirlEmptyView()
    private var pageIndex = 1
    private var dataSource: [AIGirlItemModel] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.subviews{
            backButton
            titleLabel
            tableView
            emptyView
        }
        
        backButton.leading(15).top(CGFloat.statusHeight + 5.0).size(30)
        titleLabel.centerHorizontally()
        titleLabel.CenterY == backButton.CenterY
        tableView.leading(0).trailing(0).bottom(0)
        tableView.Top == backButton.Bottom + 9
        emptyView.top(.navi_height + 140.0).leading(0).trailing(0).height(150)
        emptyView.isHidden = true
        
        tableView.refresh_loadMore {
            self.loadMoreData()
        }.refresh_pull {
            self.loadFirstPage()
        }
        loadPageData()
        backButton.addTarget(self, action: #selector(clickBackButton), for: .touchUpInside)
    }
    
    func loadFirstPage() {
        pageIndex = 1
        loadPageData()
    }
    
    func loadMoreData() {
        pageIndex += 1
        loadPageData()
    }
    
    private func loadPageData(){
        
        self.request.start(.girlList(num: pageIndex, size: 20, startIndex: 0, type: 0, isChildNode: 1), type: AIGirlListModel.self) { result in
            
            if result.isSuccessful, let model = result.model {
                if self.pageIndex == 1 {
                    self.dataSource.removeAll()
                }
                self.dataSource.append(contentsOf: model.rows)
                self.tableView.reloadData()
                self.tableView.refresh_end(model.rows.count < 20)
            }
        }
    }

}

extension AIGirlProfileGetViewController: UITableViewDelegate, UITableViewDataSource{
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        self.emptyView.isHidden = !(dataSource.count == 0 )
        return dataSource.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let model = dataSource[indexPath.row]
        var child = model.childNode
//        var firstM = AIGirlDiscoverDetailPicModel()
//        firstM.image = model.image
        child.insert(model.image, at: 0)
        
        let cell = tableView.cell(AIGirlProfileGetTableViewCell.self, indexPath: indexPath)
        cell.dataSource = child
        return cell
    }
}
